package modules.majiang.viewManager
{
	/**
	 *
	 * @author xumin.xu 2013-1-21
	 */

	import com.netease.protobuf.UInt64;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	
	import communication.majiang.MajiangMsgEvent;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablePlayerDataModel;
	import modules.majiang.data.TablesDataManager;
	
	import protobuf.MSPKG_DISCARD_NTF;
	import protobuf.MSPKG_DIST_TILES_NTF;
	import protobuf.MSPKG_SHOW_TILES_NTF;
	import protobuf.MSPKG_TILES_NTF;
	
	import utils.SmallCardFactory;

	public class HandCardManager extends ViewManager
	{
		private var tableId:UInt64;

		private var playerCardList:Array;

		private var playerCardBitmapsArray:Array = [];

		/**
		 * 胜利时牌的光效
		 **/
		private var winGlow:GlowFilter = new GlowFilter(0xffff00, 1, 20, 20, 2, 2);

		public function HandCardManager(tableId:UInt64, tableView:MovieClip)
		{
			super();
			this.tableId = tableId;

			this.mainUI = tableView;

			playerCardList = [null, null, tableView.playerCard_2, tableView.playerCard_3, tableView.playerCard_4, tableView.playerCard_5, tableView.playerCard_6];

			var i:int = 0;
			var j:int = 0;
			for (i = 2; i < playerCardList.length; i++)
			{
				var playerCard:* = playerCardList[i];
				var playerCardBitmaps:Array = [];
				for (j = 1; j <= 8; j++)
				{
					var card:* = playerCard["card_" + j];
					var cardBitmap:Bitmap = new Bitmap();
					cardBitmap.name = "cardBitmap";
					playerCardBitmaps[j] = cardBitmap;
					card.addChild(playerCardBitmaps[j]);
				}

				playerCardBitmapsArray[i] = playerCardBitmaps;
			}

			tableView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			tableView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}

		/**
		 * 所有玩家手牌
		 **/
		private function get playerTiles():Array
		{
			return tableDataModel.playerTiles;
		}

		/**
		 * 桌子数据
		 **/
		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		private function onAddToStage(event:Event):void
		{
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);

			//比赛结束10后清空桌面
			dispatcher.addEventListener(MajiangEvent.CLEAR_TABLE_INFO, function(event:GameEvent):void
			{
				if (tableId.toString() != event.data)
					return;
				updateViewData();
			});

			//赢牌的时候show自己的牌
			dispatcher.addEventListener(MSProto.MSID_SHOW_TILES_NTF.toString(), function(event:GameEvent):void
			{
				var pkg:MSPKG_SHOW_TILES_NTF = event.data;
				if (pkg.tableId.toString() != tableId.toString())
					return;
				updateViewData();
			});
			//用户弃牌广播
			dispatcher.addEventListener(MSProto.MSID_FOLD_NTF.toString(), function(event:GameEvent):void
			{
				if (tableId.toString() != event.data)
					return;
				updateViewData();
			});
			//广播某玩家手牌
			dispatcher.addEventListener(MSProto.MSID_TILES_NTF.toString(), function(event:GameEvent):void
			{
				var pkg:MSPKG_TILES_NTF = event.data;
				if (pkg.tableId.toString() != tableId.toString())
					return;
				updateViewData();
			});

			dispatcher.addEventListener(MSProto.MSID_DIST_TILES_NTF.toString(), onDistTilesNtf);

			dispatcher.addEventListener(MajiangEvent.FETCH_EFFECT_FINISH, onFetchEffectFinish);

			//其他玩家出牌的广播
			dispatcher.addEventListener(MSProto.MSID_DISCARD_NTF.toString(), onDiscardNtf);

			dispatcher.addEventListener(MajiangEvent.GET_FETCH_INFO, onGetFetchInfo);
		}

		private function removeFromStage(event:Event):void
		{
			dispatcher.removeEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);

			dispatcher.removeEventListener(MSProto.MSID_DIST_TILES_NTF.toString(), onDistTilesNtf);

			dispatcher.removeEventListener(MajiangEvent.FETCH_EFFECT_FINISH, onFetchEffectFinish);
		}

		private function onUpdateTableView(event:MajiangEvent):void
		{
			if (tableId.toString() != event.data.tableId.toString())
				return;
			updateViewData();
		}

		private function onDistTilesNtf(event:MajiangMsgEvent):void
		{
			var pkg:MSPKG_DIST_TILES_NTF = event.data;
			if (pkg.tableId.toString() != tableId.toString())
				return;

			if (event.data.tilesCount == tableDataModel.handTilesNum)
			{
				updateViewData();
			}
		}

		private function onFetchEffectFinish(event:MajiangEvent):void
		{
			var position:int = event.data.position;
			if (2 <= position && position <= 6)
			{
				updatePostion(position);
			}
		}

		private function updateViewData():void
		{
			var tablePlayerDataModel:TablePlayerDataModel;
			for (var i:int = 2; i <= 6; i++)
			{
				updatePostion(i);
			}
		}

		private function updatePostion(position:int):void
		{
			var chairId:int;
			var tiles:Array;
			var playerCard:MovieClip;

			chairId = tableDataModel.getChairIdByPosition(position);
			tiles = tableDataModel.playerTiles[chairId];
			playerCard = playerCardList[position];
			playerCard.visible = (tiles != null && tiles.length > 0);

			setCards(playerCard, tiles, position);

			//判断该位置的玩家是否有赢牌
			var isWin:Boolean = tableDataModel.isWin(chairId);
			var isLose:Boolean = tableDataModel.isLose(chairId);
			var isFold:Boolean = tableDataModel.isFold(chairId);
			var isDraw:Boolean = tableDataModel.isDraw(chairId);

			if (isWin)
			{
				playerCard.alpha = 1;
				playerCard.filters = [winGlow];

			}
			else if (isLose || isFold)
			{
				playerCard.alpha = 0.7;
				playerCard.filters = [];
			}
			else if (isDraw)
			{
				playerCard.alpha = 1;
				playerCard.filters = [winGlow];
			}
			else
			{
				playerCard.alpha = 1;
				playerCard.filters = [];
			}
		}

		/**
		 * 设置麻将的牌面
		 **/
		private function setCards(playerCard:MovieClip, tiles:Array, position:int):void
		{
			if (playerCard.visible == false)
				return;
			for (var j:int = 1; j <= 8; j++)
			{
				var card:* = playerCard["card_" + j]
				card.visible = j <= tiles.length;
				var tile:Object = tiles[j - 1];

				if (tableDataModel.handTilesNum + 1 == j)
				{
					var preCard:* = playerCard["card_" + (j - 1)];
					card.x = preCard.x + preCard.width + 8;
				}

				var cardBitmap:Bitmap = playerCardBitmapsArray[position][j];
				if (tile)
				{
					cardBitmap.bitmapData = SmallCardFactory.getNormalCardBitmapData(tile.type, tile.value);
					cardBitmap.visible = true;
				}
				else
				{
					cardBitmap.visible = false;
				}
			}
		}

		private function onGetFetchInfo(event:MajiangEvent):void
		{
			var playerCard:* = playerCardList[event.data.position];
			if (playerCard && playerCard["card_" + event.data.cardIndex])
			{
				event.data.fetchToCard = playerCard["card_" + event.data.cardIndex];
			}
		}

		/**
		 * 玩家出牌
		 **/
		private function onDiscardNtf(event:MajiangMsgEvent):void
		{
			var pkg:MSPKG_DISCARD_NTF = event.data;
			if (pkg.tableId.toString() != tableId.toString())
				return;
			updateViewData();
		}

	}
}
